import api from '@/api';
export default {
  namespace: 'usergroups',
  state: {
    sysRoots: [],
    userGroupsMenuList: [],
    userList: {
      list: [],
      pagination: {},
    },
    userGroupsList: {
      list: [],
      pagination: {},
    },
    orgUsers: [],
  },

  effects: {
    *fetchSysRoots(_, { call, put }) {
      const response = yield call(api.usergroups.querySysRoots);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveSysRoots',
          payload: response.data,
        });
      }
    },
    *fetchUserGroupsMenuList({ payload }, { call, put }) {
      const response = yield call(api.usergroups.queryUserGroupsList, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveUserGroupsMenuList',
          payload: response.data,
        });
      }
    },
    *createUserGroups({ payload }, { call, put }) {
      return yield call(api.usergroups.create, payload);
    },
    *deleteUserGroups({ payload }, { call, put }) {
      return yield call(api.usergroups.delete, payload);
    },
    *updateUserGroups({ payload }, { call, put }) {
      return yield call(api.usergroups.update, payload);
    },
    *fetchUserGroupsList({ payload }, { call, put }) {
      const response = yield call(api.usergroups.queryUsersByUserGroup, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveUserGroupsList',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
    },
    *fetchUserList({ payload }, { call, put }) {
      const response = yield call(api.usergroups.queryUsersList, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveUserList',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
    },
    *addUsers({ payload }, { call, put }) {
      return yield call(api.usergroups.addUsers, payload);
    },
    *deleteUsers({ payload }, { call, put }) {
      return yield call(api.usergroups.deleteUsers, payload);
    },
  },

  reducers: {
    saveSysRoots(state, { payload }) {
      return {
        ...state,
        sysRoots: payload,
      };
    },
    saveUserGroupsMenuList(state, { payload }) {
      return {
        ...state,
        userGroupsMenuList: payload,
      };
    },
    saveUserGroupsList(state, { payload }) {
      return {
        ...state,
        userGroupsList: payload,
      };
    },
    saveUserList(state, { payload }) {
      return {
        ...state,
        userList: payload,
      };
    },
  },
};
